package com.example.hzq.zqb.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.fragment.EnterpriseFragment1;
import com.example.hzq.zqb.fragment.EnterpriseFragment2;
import com.example.hzq.zqb.fragment.Fragment1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hzq on 2017/7/11.
 */

public class EnterpriseActivity extends AppCompatActivity {
    private Button f1,f2;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private int flag;
    private ImageButton back;
    private TextView top;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enterprise);
        init();
        initEvents();
        initFragment();
        flag=0;
    }
    private void init() {
        f1=(Button)findViewById(R.id.enter_f1);
        f2=(Button)findViewById(R.id.enter_f2);
        vp=(ViewPager)findViewById(R.id.enter_viewpage);
        back=(ImageButton)findViewById(R.id.back1);
        top=(TextView)findViewById(R.id.toptext);
        top.setText("企业兼职");
    }
    View.OnClickListener onClickListener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            reset();
            switch (v.getId()) {
                case R.id.enter_f1:
                    flag = 0;
                    selectTab(0);
                    break;
                case R.id.enter_f2:
                    flag = 1;
                    selectTab(1);
                    break;
                case R.id.back1:
                    finish();
                    break;
            }
        }
    };
    private void initEvents() {
        f1.setOnClickListener(onClickListener);
        f2.setOnClickListener(onClickListener);
        back.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new EnterpriseFragment1());
        listfragment.add(new EnterpriseFragment2());
        fpadapter= new  FragmentPagerAdapter(getSupportFragmentManager())  {
            @Override
            public int getCount() {
                return listfragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                reset();
                selectTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private  void reset()
    {
        f1.setTextColor(0xff000000);
        f2.setTextColor(0xff000000);
        f1.setBackgroundColor(0xffffffff);
        f2.setBackgroundColor(0xffffffff);
    }
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                f1.setTextColor(0xffffffff);
                f1.setBackgroundColor(0xff008dff);
                top.setText("企业兼职");
                break;
            case 1:
                f2.setTextColor(0xffffffff);
                f2.setBackgroundColor(0xff008dff);
                top.setText("个人中心");
                break;
        }
        vp.setCurrentItem(i);
    }
}

